import * as THREE from "three"
import gsap from "gsap"

export default function modifyCityMaterial(mesh) {
  mesh.material.onBeforeCompile = (shader) => {
    // console.log(shader.fragmentShader, 'fragmentShader'); // 片元着色器
    // console.log(shader.vertexShader, 'vertexShader'); // 顶点着色器
    shader.fragmentShader = shader.fragmentShader.replace(
      "#include <dithering_fragment>",
      `
        #include <dithering_fragment>
        // #end#
      `
    )
    aadGradColor(shader, mesh)
    aadSpread(shader)
    aadLightLine(shader)
    aadToTopLine(shader)
  }
}

export function aadGradColor(shader, mesh) {
  mesh.geometry.computeBoundingBox()
  // 获取物体的高度差
  let { max, min } = mesh.geometry.boundingBox
  let uHeight = max.y - min.y
  shader.uniforms.uTopColor = {
    value: new THREE.Color("#aaaeff")
  }
  shader.uniforms.uHeight = {
    value: uHeight
  }
  shader.vertexShader = shader.vertexShader.replace(
    "#include <common>",
    `
      #include <common>
      varying vec3 vPosition;
    `
  )
  shader.vertexShader = shader.vertexShader.replace(
    "#include <begin_vertex>",
    `
      #include <begin_vertex>
      vPosition = position;
    `
  )
  shader.fragmentShader = shader.fragmentShader.replace(
    "#include <common>",
    `
      #include <common>
      uniform vec3 uTopColor;
      uniform float uHeight;
      varying vec3 vPosition;
    `
  )
  shader.fragmentShader = shader.fragmentShader.replace(
    "// #end#",
    `
      vec4 distGradColor = gl_FragColor;
      // 设置混合的百分比
      float gradMix = (vPosition.y + uHeight/2.0)/uHeight;
      // 计算出混合颜色
      vec3 gradMixColor = mix(distGradColor.xyz, uTopColor, gradMix);
      gl_FragColor = vec4(gradMixColor, 1);
      // #end#
    `
  )
}
// 添加光圈扩散
export function aadSpread(shader) {
  // 设置扩散的中心点
  shader.uniforms.uSpreadCenter = {
    value: new THREE.Vector2(0, 0)
  };
  // 扩散的时间
  shader.uniforms.uSpreadTime = { value: 0 };
  // 设置线圈的宽度
  shader.uniforms.uSpreadWidth = { value: 40 };
  // 定义光圈值
  shader.fragmentShader = shader.fragmentShader.replace(
    "#include <common>",
    `
      #include <common>
      uniform vec2 uSpreadCenter;
      uniform float uSpreadTime;
      uniform float uSpreadWidth;
    `
  )
  shader.fragmentShader = shader.fragmentShader.replace(
    "// #end#",
    `
      float spreadRadius = distance(vPosition.xz, uSpreadCenter);
      // 扩散范围的函数
      float spreadIndex = -(spreadRadius - uSpreadTime)*(spreadRadius - uSpreadTime)+uSpreadWidth;
      if(spreadIndex > 0.0) {
        gl_FragColor = mix(gl_FragColor, vec4(1,1,1,1), spreadIndex/uSpreadWidth);
      }
      // #end#
    `
  )
  // 光圈扩散动画
  gsap.to(shader.uniforms.uSpreadTime, {
    value: 1000,
    duration: 2,
    ease: "none",
    repeat: -1
  })
}
// 直线扫描
export function aadLightLine(shader) {
  // 扩散的时间
  shader.uniforms.uLightLineTime = { value: -1000 };
  // 设置线的宽度
  shader.uniforms.uLightLineWidth = { value: 80 };
  // 定义光圈值
  shader.fragmentShader = shader.fragmentShader.replace(
    "#include <common>",
    `
      #include <common>
      uniform float uLightLineTime;
      uniform float uLightLineWidth;
    `
  )
  shader.fragmentShader = shader.fragmentShader.replace(
    "// #end#",
    `
      // 直线扫描的函数
      float lightLineMix = -(vPosition.x + vPosition.z - uLightLineTime)*(vPosition.x + vPosition.z - uLightLineTime)+uLightLineWidth;
      if(lightLineMix > 0.0) {
        gl_FragColor = mix(gl_FragColor, vec4(0.8,1,1,1), lightLineMix/uLightLineWidth);
      }
      // #end#
    `
  )
  // 光圈扩散动画
  gsap.to(shader.uniforms.uLightLineTime, {
    value: 1000,
    duration: 4,
    ease: "none",
    repeat: -1
  })
}
// 向上扫描
export function aadToTopLine(shader) {
  // 扩散的时间
  shader.uniforms.uToTopLineTime = { value: 0 };
  // 设置线的宽度
  shader.uniforms.uToTopLineWidth = { value: 100 };
  // 定义光圈值
  shader.fragmentShader = shader.fragmentShader.replace(
    "#include <common>",
    `
      #include <common>
      uniform float uToTopLineTime;
      uniform float uToTopLineWidth;
    `
  )
  shader.fragmentShader = shader.fragmentShader.replace(
    "// #end#",
    `
      // 直线扫描的函数
      float toTopLineMix = -(vPosition.y - uToTopLineTime)*(vPosition.y - uToTopLineTime)+uToTopLineWidth;
      if(toTopLineMix > 0.0) {
        gl_FragColor = mix(gl_FragColor, vec4(0.8,0.8,1,1), toTopLineMix/uToTopLineWidth);
      }
      // #end#
    `
  )
  // 光圈扩散动画
  gsap.to(shader.uniforms.uToTopLineTime, {
    value: 500,
    duration: 3,
    ease: "none",
    repeat: -1
  })
}